<?php

/**
 * 数据操作类
 * @author wangzhongbin
 */
class Database {
	private $host;
	private $username;
	private $password;
	private $dbname;
	private $port;
	private $connect;
	private $isOpen;
	private $debugstate;

	/**
	 * 获取数据库连接是否打开
	 * @return boot 返回数据库连接是否打开
	 * */
	public function getIsOpen() {
		return $this->isOpen;
	}

	/**
	 * 获取数据库链接
	 * @return 返回数据库链接
	 * */
	public function getConnect() {
		$this->open();
		return $this->connect;
	}

	/**
	 * 是否调试状态
	 * @return 返回调试状态
	 * */
	public function getDebugState() {
		return $this->debugstate;
	}

	/**
	 * 设置调试状态
	 * @param bool $debugstate 设置调试状态
	 * */
	public function setDebugState($debugstate) {
		$this->debugstate = $debugstate;
	}

	/**
	 * 构造函数
	 * @param string $host 服务器地址
	 * @param string $dbname 数据库
	 * @param string $username 用户名
	 * @param string $password 密码
	 * @param int $port 端口
	 * */
	public function __construct($host, $dbname, $username, $password, $port = 3306) {
		$this->host = $host;
		$this->dbname = $dbname;
		$this->username = $username;
		$this->password = $password;
		$this->port = $port;
		$this->isOpen = false;
		$this->debug = false;
		$this->init();
	}

	/**
	 * 初始化
	 * @return 返回数据库链接
	 * */
	private function init() {
		$this->connect = new mysqli($this->host, $this->username, $this->password, $this->dbname, $this->port);
		if (mysqli_connect_errno()) {
			$this->message('Can not connect to MySQL server');
		}
		$this->connect->query("set session sql_mode='NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'"); //NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
		$this->connect->query("set names 'utf8'");
		$this->isOpen = true;
	}

	/**
	 * 调试
	 * @return 字符
	 * */
	private function debug($string) {
		if ($this->debugstate)
			echo $string . "<br />";
	}

	/**
	 *关闭数据连接 
	 */
	public function close() {
		if ($this->isOpen)
			@ $this->connect->close();
		//@mysqli_close ( $this->connect );
		$this->isOpen = false;
	}

	/**
	 *打开数据连接 
	 */
	public function open() {
		if (!$this->isOpen)
			$this->init();
	}

	/**
	 *析构函数 
	 */
	public function __destruct() {
		$this->close();
		unset ($this->host);
		unset ($this->dbname);
		unset ($this->username);
		unset ($this->password);
		unset ($this->port);
		unset ($this->isOpen);
		//$this->null;
	}

	/**
	 * 特殊字符转义
	 * @param 要转义的字符
	 * @return string 转义后的字符
	 */
	public function escape($string) {
		$this->open();
		return $this->connect->real_escape_string($string);
	}

	/**
	 * 执行查询返回数组
	 * @param $sql sql语句
	 * @param $result_type 是一个常量，可以接受以下值：MYSQL_ASSOC，MYSQL_NUM 和 MYSQL_BOTH
	 * @return array 数组
	 */
	public function query($sql, $result_type = MYSQLI_ASSOC) {
		$this->open();
		$this->debug($sql);
		$rows = array ();
		if ($result = $this->connect->query($sql)) {
			while ($row = $result->fetch_array($result_type)) {
				array_push($rows, $row);
			}
			$result->free();
		} else {
			$this->message('', $sql);
		}
		//$this->close();
		return $rows;
	}
	/**
	 * 执行sql语句 update or insert or delete
	 */
	public function execute($sql) {
		$this->open();
		$this->debug($sql);
		return $this->connect->query($sql);
	}

	/**
	 * 执行查询，并返回查询所返回的结果集中第一行的第一列。忽略其他列或行
	 * @param $sql sql语句
	 * @return object
	*/
	public function scalar($sql) {
		$this->open();
		$this->debug($sql);
		$row = array ();
		$scalar = null;
		if ($result = $this->connect->query($sql)) {
			$row = $result->fetch_row();
			$result->free();
			$scalar = $row[0];
		} else {
			$this->message('', $sql);
		}
		//$this->close();
		return $scalar;
	}

	/**
	 * 执行查询存储过程返回数组
	 * @param $sql 存储过程语句
	 * @param $result_type 是一个常量，可以接受以下值：MYSQL_ASSOC，MYSQL_NUM 和 MYSQL_BOTH
	 * @return array 数组
	 */
	public function queryCall($sql, $result_type = MYSQLI_ASSOC) {
		$this->open();
		$this->debug($sql);
		$result = null;
		if ($this->connect->real_query($sql))
			$result = $this->result($result_type);
		else
			$this->message('', $sql);
		//$this->close();
		return $result;
	}

	/**
	 * 返回执行存储过程后的结果
	 * @param $result_type 是一个常量，可以接受以下值：MYSQL_ASSOC，MYSQL_NUM 和 MYSQL_BOTH
	 * @return array 数组
	 */
	public function result($result_type = MYSQLI_ASSOC) {
		$rows = array ();
		do {
			if ($result = $this->getConnect()->store_result()) {
				while ($row = $result->fetch_array($result_type)) {
					array_push($rows, $row);
				}
				$result->free();
			}
		}
		while ($this->getConnect()->next_result());
		return $rows;
	}

	private function error() {
		return (($this->connect) ? mysqli_error($this->connect) : mysqli_error());
	}

	private function errno() {
		return intval(($this->connect) ? mysqli_errno($this->connect) : mysqli_errno());
	}

	/**
	 * 消息显示
	 */
	private function message($message = '', $sql = '') {
		include 'Database.error.php';
	}

	/**
	 * Executes a query that has been previously prepared using the mysqli_prepare() function
	 * @param Procedural style only: A statement identifier returned by mysqli_stmt_init()
	 * @return unknown
	 */
	public function stmt_execute($stmt) {
		$result = $stmt->execute();
		if (!$result) {
			$this->stmt_message($stmt);
		}
		$stmt->close();
		//$this->close();
		return $result;
	}

	/**
	 * 消息显示
	 */
	private function stmt_message($stmt, $message = '', $sql = '') {
		include 'Database_stmt.error.php';
	}
}
//./mysqladmin -u root -p flush-tables
?>